package com.svse.controller;

import com.svse.annotation.DataSource;
import com.svse.annotation.OperateLog;
import com.svse.annotation.SubmitLock;
import com.svse.dao.DemoRepository;
import com.svse.entity.DemoEntity;
import com.svse.entity.DemoJpaEntity;
import com.svse.entity.UserEntity;
import com.svse.enums.DataSourceEnum;
import com.svse.services.DemoService;
import com.svse.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Copyright 上海危网信息科技有限公司版权所有
 * @company wwsoft
 * @Date 2019/11/20 14:16
 */
@RestController
@RequestMapping("/multipartDatasource")
public class MultipartDataSourceController {
    @Autowired
    private UserService userService;
    @Autowired
    private DemoService demoService;

    @RequestMapping(value = "/findList", method = RequestMethod.POST, produces = "application/json")
    @OperateLog(description = "sys数据库的user")
    public List<UserEntity> findList() {
        return userService.list();
    }


    @Transactional(rollbackFor=Exception.class)
    @RequestMapping(value = "/rollBackTest")
    @OperateLog(description = "多数据源回滚")
    @DataSource(DataSourceEnum.SLAVE)
    public void rollBackTest(){
        UserEntity userEntity=new UserEntity();
        userEntity.setAge(11);
        userEntity.setName("回滚测试");
        System.out.println(userEntity.toString());
        userService.save(userEntity);
    }
    @Transactional(rollbackFor=Exception.class)
    @RequestMapping(value = "/rollBackTest2")
    @OperateLog(description = "多数据源回滚")
    @DataSource(DataSourceEnum.MASTER)
    public void rollBackTest2(){
        DemoEntity demoEntity=new DemoEntity();
        demoEntity.setAge(20);
        demoEntity.setName("测试数据");
        demoEntity.setContent("测试数据");
        demoEntity.setSex("男");
        System.out.println(demoEntity.toString());
        demoService.save(demoEntity);
        String str= null;
        System.out.println(str.toString());
        demoService.save(demoEntity);
    }


    @RequestMapping("/findAll")
    @OperateLog(description = "test数据库的demo")
    public List<DemoEntity> list() {
        return demoService.list();
    }

    @RequestMapping("/test")
    @SubmitLock
    public String test(String json){
        return json;
    }
}
